package main

import (
	"github.com/fsnotify/fsnotify"
	"goFNotify/config"
	"goFNotify/global"
	"log"
)

func main() {
	config.GetConfig()
	//创建一个监控对象
	watch, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watch.Close()
	//添加要监控的对象，文件或文件夹
	for _, v := range global.CONFIG.WatchPath {
		err = watch.Add(v)
		if err != nil {
			log.Fatal(err)
		}
	}
	//我们另启一个goroutine来处理监控对象的事件
	go func() {
		for {
			select {
			case ev := <-watch.Events:
				{
					if ev.Op&fsnotify.Create == fsnotify.Create {
						global.Command("sh " + global.CONFIG.Sh)
						log.Println("创建文件 : ", ev.Name)
					}
					if ev.Op&fsnotify.Write == fsnotify.Write {
						global.Command("sh " + global.CONFIG.Sh)
						log.Println("写入文件 : ", ev.Name)
					}
					if ev.Op&fsnotify.Remove == fsnotify.Remove {
						global.Command("sh " + global.CONFIG.Sh)
						log.Println("删除文件 : ", ev.Name)
					}
					if ev.Op&fsnotify.Rename == fsnotify.Rename {
						global.Command("sh " + global.CONFIG.Sh)
						log.Println("重命名文件 : ", ev.Name)
					}
					if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
						global.Command("sh " + global.CONFIG.Sh)
						log.Println("修改权限 : ", ev.Name)
					}
				}
			case err := <-watch.Errors:
				{
					log.Println("error : ", err)
					return
				}
			}
		}
	}()
	//循环
	select {}
}
